#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <map>
#include <cmath>
using namespace std;



bool check(string str)
{
    bool is_right = true;
    int left_bracket = 0;  //左括号的数量
    int right_bracket = 0; //右括号的数量
    for (int i=0; i<str.length(); i++)
    {
        if(str[i] == '(')
        {
            left_bracket++;
        }
        else if(str[i] == ')')
        {
            right_bracket++;
        }
        else if((str[i]<'0' || str[i]>'9') & str[i]!='+' & str[i]!='-' & str[i]!='*' & str[i]!='/' & str[i]!='^' & str[i]!='.' & str[i]!='(' & str[i]!=')')
        {
            //可能错误1：非法字符
            cout<<"Please check your input! Invalid characters exist in the expression"<<endl;
            is_right = false;
        }
    }
    if (left_bracket != right_bracket)
    {
        //可能错误3：左右括号数量不同
        cout<<"Please check your input! The number of right brackts is expected to equal to the left."<<endl;
        is_right = false;
    }

    for (int i=0; i<str.length(); i++)
    {
        if (str[i] == '.' )
        {
            while(string("+-/*^()").find(str[i+1])==string::npos)
            {   //可能错误2：一个数字中出现了大于一个小数点
                if (str[i+1]=='.')
                {
                    cout<<"Please check you input! There may be a problem with the decimal point"<<endl;
                    is_right = false;
                }
                i++;
            }            
        }
    }
    return is_right;
}

double calculate(const vector<string> ans)
{
    cout<<endl;
    stack<double> mystack; //用来存数据
    double num;
    for (int i = 0; i < ans.size(); i++)
    {
        string temp = ans[i];
        if(string("+-/*^").find(temp) == string::npos) //如果是数字的话，入栈
        {
            mystack.push(stod(temp));
        }
        else //如果是运算符的话，则从栈中弹出数字进行计算
        {
            double secondnum = mystack.top(); //取出栈顶元素
            mystack.pop(); //栈顶元素出栈
            double firstnum = mystack.top(); //取出栈顶元素
            mystack.pop(); //栈顶元素出栈
            if (temp == "+")
            {
                num = firstnum + secondnum;
                mystack.push(num);
            }
            else if (temp == "-")
            {
                num = firstnum - secondnum;
                mystack.push(num);
            }
            else if (temp == "*")
            {
                num = firstnum * secondnum;
                mystack.push(num);
            }
            else if (temp == "/" & secondnum != 0)
            {
                num = firstnum / secondnum;
                mystack.push(num);
            }
            else if (temp == "/" & secondnum == 0)
            {
                cout<<"Error: divide by zero!"<<endl;
            }
            else 
            {
                num = pow(firstnum, secondnum);
                mystack.push(num);
            }

        }
    }
    return mystack.top(); //遍历完后缀表达式后，栈中的元素即为计算结果
}

